package io.union_pay.utils;

import okhttp3.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class OkHttpUtils {
    public static Response postJson(String url, String json) {
        return postJson(url, json, 10);
    }

    public static Response postJson(String url, String json, int timeOut) {
        return postJson(url, json, null, timeOut);
    }

    public static Response postJson(String url, String json, Map<String, Object> headers) {
        return postJson(url, json, headers, 10);
    }

    public static Response postJson(String url, String json, Map<String, Object> headers, int timeOut) {
        Response response = null;
        try {
            //1,构建 client
            OkHttpClient client = new OkHttpClient.Builder().readTimeout(timeOut, TimeUnit.SECONDS).build();//可以通过添加各种属性
            //2，构建request请求
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
            Request.Builder builder = new Request.Builder().url(url).post(requestBody);//可以继续添加各种数据

            if (headers != null) {
                for (String key : headers.keySet()) {
                    if (headers.get(key) != null) {
                        builder.addHeader(key, String.valueOf(headers.get(key)));
                    }
                }
            }

            Request request = builder.build();
                    //3，获得call对象
            Call call = client.newCall(request);
            //4,执行同步请求
            response = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }


    public static Response get(String url, Map<String, Object> params, int timeOut) {
        Response response = null;
        try {
            //1,构建 client
            OkHttpClient client = new OkHttpClient.Builder().readTimeout(timeOut, TimeUnit.SECONDS).build();//可以通过添加各种属性
            //2，构建request请求
            if (params != null && !params.isEmpty()) {
                url = url + "?" + getParamStr(params);
            }
            Request request = new Request.Builder().url(url).get().build();//可以继续添加各种数据
            //3，获得call对象
            Call call = client.newCall(request);
            //4,执行同步请求
            response = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }


    private static String getParamStr(Map<String, Object> params) throws UnsupportedEncodingException {
        StringBuilder paramBuilder = new StringBuilder("");
        for (String paramKey : params.keySet()) {
            if (params.get(paramKey) == null) {
                continue;
            }
            if (paramBuilder.length() > 0) {
                paramBuilder.append("&");
            }
            paramBuilder.append(paramKey);
            paramBuilder.append("=");
            paramBuilder.append(URLEncoder.encode(String.valueOf(params.get(paramKey)), "utf-8"));
        }
        return paramBuilder.toString();
    }

    public interface RequestSuccess {
        void success(Response response) throws IOException;
    }
}
